﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace WebPortal
{
    public static class MvcHtmlStringExtension
    {
        /// <summary>
        /// @Html.TextBoxFor(m => m.UserName).DisableIf(()=> Model.IsEditMode==false)  
        /// </summary>
        /// <param name="htmlString"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static MvcHtmlString DisableIf(this MvcHtmlString htmlString, Func<bool> expression)
        {
            if (expression.Invoke())
            {
                var html = htmlString.ToString();
                const string disabled = "\"disabled\"";
                html = html.Insert(html.IndexOf(">", StringComparison.Ordinal), " disabled= " + disabled);
                return new MvcHtmlString(html);
            }
            return htmlString;
        }

        public static MvcHtmlString Width(this MvcHtmlString htmlString, int width)
        {
            if (width > 0)
            {
                var html = htmlString.ToString();
                string strWidth = string.Format("\"{0}px\"", width);
                html = html.Insert(html.IndexOf(">", StringComparison.Ordinal), " Width= " + strWidth);
                return new MvcHtmlString(html);
            }
            return htmlString;
        }
    }
}
